package src.com.syx.queue;

import java.sql.SQLOutput;

public class ArrayQueueDemo01{
    public static void main(String[] args) {
        ArrayQueue queue = new ArrayQueue(5);

        // 入队测试
        queue.enqueue(10);
        queue.enqueue(20);
        queue.enqueue(30);
        queue.enqueue(40);
        queue.enqueue(50);
        queue.enqueue(60); // 测试队列满的情况

        // 显示队列
        System.out.println("当前队列内容:");
        queue.list();

        // 出队测试
        System.out.println("出队元素: " + queue.dequeue());
        System.out.println("出队元素: " + queue.dequeue());

        // 显示队首元素
        System.out.println("当前队首元素: " + queue.peek());

        // 再次显示队列
        System.out.println("当前队列内容:");
        queue.list();
    }
}

// 使用数组模拟队列
class ArrayQueue {
    private int maxSize;
    private int front; // 指向队列头的前一个位置
    private int rear;
    private int[] arr;

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.front = -1;
        this.rear = -1;
        this.arr = new int[maxSize];
    }

    // 1. 判断队列是否满
    public boolean isFull() {
        return this.rear == maxSize - 1;
    }

    // 2. 判断队列是否为空
    public boolean isEmpty() {
        return this.rear == this.front;
    }

    // 3. 入队
    public void enqueue(int n) {
        if (isFull()) {
            System.out.println("队列满，不能加入数据...");
            return;
        }
        rear++;
        this.arr[rear] = n;
    }


    // 4. 出队
    public int dequeue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空...");
        }
        return this.arr[++front];
    }


    // 5. 显示队列所有数据
    public void list() {
        if (isEmpty()) {
            System.out.println("队列为空...");
            return;
        }
        for (int i = front+1 ; i <= rear; i++) {
            System.out.printf("arr[%d]=%d\n", i, arr[i]);
        }
    }

    // 6. 显示队列头数据（不出队）
    public int peek() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空...");
        }
        return arr[front + 1];
    }

}
